home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / packfile.ps < prev    next >
Text File  |  1996-01-10  |  11KB  |  335 lines

  1. %    Copyright (C) 1994, 1995 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % packfile.ps
  16. % Pack groups of files together, with compression, for use in
  17. % storage-scarce environments.
  18.  
  19. % ****** NOTE: This file must be kept consistent with gs_pfile.ps.
  20.  
  21. % ---------------- Huffman coding utilities ---------------- %
  22.  
  23. % We count runs of zeros, and individual byte frequencies separately
  24. % depending on whether they follow or do not follow a run of zeros.
  25. /zruns 256 array def
  26. /zfreq 256 array def
  27. /nzfreq 256 array def
  28. /maxcode 13 def        % max code length, must be between 10 and 16
  29. /maxzrun 100 def    % max length of zero run, must be <= 100
  30. /statbuf 10000 string def
  31.  
  32. % Initialize statistics.
  33. /initstats        % - initstats -
  34.  { 0 1 255 { zruns exch 0 put } for
  35.    0 1 255 { zfreq exch 0 put } for
  36.    0 1 255 { nzfreq exch 0 put } for
  37.  } bind def
  38.  
  39. % Accumulate statistics from an individual file.
  40. /addstats        % <file> addstats -
  41.  { 0
  42.     { 1 index //statbuf readstring 3 1 roll
  43.     % Stack: file eof numzeros data
  44.        { dup 0 eq
  45.       { pop 1 add
  46.       }
  47.       { 1 index 0 ne
  48.          { exch 255 min
  49.            //zruns exch 2 copy get 1 add put
  50.            0 exch //zfreq
  51.          }
  52.          { //nzfreq
  53.          }
  54.         ifelse
  55.         exch 2 copy get 1 add put
  56.       }
  57.      ifelse
  58.        } forall
  59.       exch not { exit } if (.) print flush
  60.     } loop
  61.    pop closefile
  62.  } bind def
  63.  
  64. % Compute the Huffman codes from the statistics.
  65. /statcodes        % - statcodes <array>
  66.  { maxcode 1 add 256 add maxzrun 2 sub add 1 add array    % full array
  67.    dup maxcode 1 add dup 2 index length exch sub getinterval    % data
  68.     % Put statistics into array
  69.    dup 0 1 255
  70.     { zfreq 1 index get nzfreq 2 index get add put dup
  71.     } for
  72.    0 zruns 1 get put
  73.    256 zruns 2 maxzrun 2 sub getinterval putinterval
  74.    dup dup length 1 sub 1 put    % EOD
  75.    maxcode .computecodes
  76.  } bind def
  77.  
  78. % ---------------- File handling ---------------- %
  79.  
  80. % Copy one file to another.
  81. % Close the input file, but not the output file.
  82. /copyfile        % <infile> <outfile> copyfile <outfile> <length>
  83.  { 0 mark statbuf
  84.     { 4 index 1 index readstring
  85.       exch 5 index 1 index writestring
  86.       length 5 -1 roll add 4 1 roll
  87.       not { exit } if (.) print flush
  88.     } loop
  89.    cleartomark 3 -1 roll closefile dup == flush
  90.  } bind def
  91.  
  92. % Represent a Type 1 font in its most compressed format.
  93. % Requires -dWRITESYSTEMDICT to run.
  94. % Does not close the output file.
  95. (wrfont.ps) run
  96. /compressfont        % <fontname> <outfile> compressfont <outfile>
  97.  { exch save
  98.    systemdict /executeonly /readonly load put
  99.    systemdict /noaccess /readonly load put
  100.    systemdict readonly pop
  101.    wrfont_dict begin
  102.      /binary_CharStrings true def
  103.      /binary_tokens true def
  104.      /encrypt_CharStrings false def
  105.      /standard_only false def
  106.      /use_lenIV 0 def
  107.      /smallest_output true def
  108.    end
  109.    exch findfont setfont
  110.    1 index writefont
  111.    restore
  112.  } bind def
  113.  
  114. % ---------------- Main program ---------------- %
  115.  
  116. % Find the length of a file.
  117. /filelength        % <filename> filelength <length>
  118.  { status { pop pop exch pop } { -1 } ifelse
  119.  } bind def
  120.  
  121. % Define the header string for a compressed file.
  122. /beginfilestring
  123. ({dup token pop exch[/MaxCodeLength 2 index token pop/Tables 4 index token pop
  124. /EndOfData true/EncodeZeroRuns 256 .dicttomark
  125. /BoundedHuffmanDecode filter/MoveToFrontDecode filter
  126. [/BlockSize 4 -1 roll .dicttomark/BWBlockSortDecode filter
  127. }) readonly def
  128.  
  129. % Write a 16-bit big-endian non-negative integer on a file.
  130. /write16        % <file> <int> write16 -
  131.  { 2 copy -8 bitshift write 255 and write
  132.  } bind def
  133.  
  134. % Compress a group of files together.
  135. % Return a dictionary in which the keys are the input file names
  136. % and the values are [startpos length] in the uncompressed concatenation.
  137. % Does not open or close the output file.
  138. /tempname (t.em) def
  139. /packfiles        % <filenames> <outfile> packfiles <outfile> <posdict>
  140.  {    % Concatenate files to a temporary file.
  141.    tempname (w) file
  142.    dup /MoveToFrontEncode filter
  143.    dup <<
  144.     /BlockSize 1000000
  145.    >> /BWBlockSortEncode filter
  146.         % Stack: filenames outfile tempfile mtfe bwe
  147.    5 -1 roll dup length dict 0 6 2 roll
  148.     {        % Stack: outfile posdict pos tempfile mtfe bwe infilename
  149.       dup ==only dup (r) file 2 index copyfile exch pop
  150.       dup 7 index 4 2 roll 7 index exch 2 array astore put
  151.       5 -1 roll add 4 1 roll
  152.     } forall
  153.    closefile closefile closefile pop exch
  154.         % Stack: posdict outfile
  155.     % Compute an optimal Huffman code.
  156.    initstats
  157.    tempname (r) file addstats
  158.     % Actually compress the file.
  159.     % Write the decompression information on the output first.
  160.    dup tempname filelength write==
  161.    dup maxcode write==
  162.     % Write the code table as a homogenous number array.
  163.    statcodes exch
  164.      dup 149 write dup 32 write dup 2 index length write16
  165.      exch { 2 copy write16 pop } forall
  166.    dup <<
  167.     /MaxCodeLength maxcode
  168.     /EndOfData true
  169.     /EncodeZeroRuns 256
  170.     /Tables statcodes
  171.    >> /BoundedHuffmanEncode filter
  172.    tempname (r) file exch copyfile pop closefile
  173.    exch
  174.  } bind def
  175.  
  176. % Squeeze a font to a .cpf file in anticipation of compression.
  177. /squeezefont        % <fontname> squeezefont <filename.cpf>
  178.  { dup type /nametype ne { cvn } if
  179.    dup
  180.     { dup type /stringtype eq { exit } if
  181.       Fontmap exch get
  182.     }
  183.    loop
  184.         % Stack: fontname filename
  185.    dup dup
  186.     { (.) search not { exit } if
  187.       exch pop exch 3 -1 roll pop
  188.     }
  189.    loop
  190.         % Stack: fontname filename noextname extension
  191.    exch
  192.     { (/) search not { (\\) search not { exit } if } if
  193.       pop pop
  194.     }
  195.    loop
  196.         % If the font extension is anything other than
  197.         % .pfa or .pfb, we assume it can't be rewritten
  198.         % using compressfont.
  199.         % Stack: fontname filename extension basename
  200.    (.cpf) concatstrings dup 5 1 roll (w) file
  201.         % Stack: outfilename fontname filename extension outfile
  202.    exch dup (pfa) eq exch (pfb) eq or
  203.         % Stack: outfilename fontname filename outfile bool
  204.     { exch pop compressfont
  205.     }
  206.     { 3 -1 roll pop
  207.       exch findlibfile pop exch pop
  208.       exch copyfile pop
  209.     }
  210.    ifelse closefile
  211.  } bind def
  212.  
  213. % ---------------- Production code ---------------- %
  214.  
  215. % The following code constructs a packed version of the commercial-quality
  216. % fonts available from Aladdin Enterprises.  To use this code:
  217. %    - If desired, change the output file names below.
  218. %    - Make sure you have the synthetic font data (fontmap.shs and the
  219. %      *.ps files for the commercial fonts) in a directory that is on
  220. %      Ghostscript's search path.
  221. %    - Construct the packed fonts by running
  222. %        gs -dNODISPLAY -dWRITESYSTEMDICT packfile.ps
  223. %    - If desired, move the output files to the directory that will be
  224. %      used at run time.  You no longer need the *.pfb or *.ps files
  225. %      for the original fonts; however, you do still need the Fontmap
  226. %      for these fonts, because it defines the font name aliases.
  227. %    - Add the following line to the end of gs_fonts.ps:
  228. %        (ALL.cmp) run
  229. %      (substituting the definition of allmapname if you changed it).
  230.  
  231. % Define the output file names.  The extensions are arbitrary;
  232. % any legal file name is allowed.
  233. /allname (ALL.cff) def        % the compressed font file
  234. /allmapname (ALL.cmp) def    % the Fontmap override file
  235.  
  236. % Load an alternate Fontmap that references the synthetic oblique and
  237. % narrow fonts.
  238. true .setglobal
  239. (fontmap.shs) findlibfile pop exch pop .loadFontmap
  240. false .setglobal
  241.  
  242. % Define the packaging of fonts into font groups.
  243. % Fewer larger groups compress better, but make decompression slower.
  244. /Lists [
  245. [    % The oblique and narrow fonts are synthetic,
  246.     % and take very little space.
  247.   /AvantGarde-BookOblique /AvantGarde-DemiOblique
  248.   /Courier-Oblique /Courier-BoldOblique
  249.   /Helvetica-Oblique /Helvetica-BoldOblique
  250.   /Helvetica-Narrow /Helvetica-Narrow-Oblique
  251.   /Helvetica-Narrow-Bold /Helvetica-Narrow-BoldOblique
  252. ]
  253. [/AvantGarde-Book /AvantGarde-Demi
  254.  /Bookman-Light] [/Bookman-LightItalic
  255.  /Bookman-Demi /Bookman-DemiItalic
  256.  /Courier] [/Courier-Bold
  257.  /Helvetica /Helvetica-Bold]
  258. [/NewCenturySchlbk-Roman /NewCenturySchlbk-Italic
  259.  /NewCenturySchlbk-Bold /NewCenturySchlbk-BoldItalic]
  260. [/Palatino-Roman /Palatino-Italic
  261.  /Palatino-Bold /Palatino-BoldItalic]
  262. [/Times-Roman /Times-Italic
  263.  /Times-Bold /Times-BoldItalic]
  264. [/Symbol
  265.  /ZapfChancery-MediumItalic
  266.  /ZapfDingbats]
  267. ] def
  268.  
  269. % We need to register the fonts under their true names, not aliases.
  270. /Lists Lists mark exch
  271.  { mark exch
  272.     {  { Fontmap 1 index get dup type /nametype ne { pop exit } if
  273.      exch pop
  274.        }
  275.       loop
  276.     }
  277.    forall ]
  278.  }
  279. forall ] def
  280.  
  281. % Squeeze the fonts to their .cpf format.
  282. (Squeezing... ) print flush
  283. /fdict mark
  284. Lists
  285.  { { dup squeezefont } forall } forall
  286. .dicttomark def
  287. (done.\n) print flush
  288.  
  289. % Invert fdict.
  290. /f2dict fdict length dict def
  291. fdict { exch f2dict 3 1 roll put } forall
  292.  
  293. % Construct the compressed font file.
  294. (Creating ) print allname print (... ) print flush
  295. /posdict fdict length dict def
  296. /all allname (w) file def
  297. all beginfilestring writestring
  298. Lists
  299.  { dup == flush
  300.    /fbegin all fileposition def
  301.    mark exch { fdict exch get } forall ]
  302.    all packfiles exch pop
  303.    /flength all fileposition fbegin sub def
  304.     { fbegin flength 3 -1 roll aload pop 4 packedarray
  305.       exch f2dict exch get exch posdict 3 1 roll put
  306.     }
  307.    forall
  308.  }
  309. forall
  310. all closefile
  311. (done.\n) print flush
  312.  
  313. % Write the Fontmap addendum for accessing compressed fonts.
  314. (Writing ) print allmapname print (... ) print flush
  315. allmapname (w) file
  316. dup (%!
  317. /.runpackedlibfile where{pop}{(gs_pfile.ps)runlibfile}ifelse
  318. .currentglobal true .setglobal
  319. ) writestring
  320. posdict
  321.  { exch 2 index exch write==only exch dup ({) writestring
  322.    dup allname write==only
  323.    exch { 1 index dup ( ) writestring exch write==only } forall
  324.    dup ( .runpackedlibfile}bind .definefontmap
  325. ) writestring
  326.  }
  327. forall
  328. dup (.setglobal
  329. ) writestring
  330. closefile
  331. (done.\n) print flush
  332.